import 'package:flutter/material.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/http/dio_manager.dart';
import 'package:flutter2/model/order_entity.dart';
import 'package:flutter2/ui/add_card.dart';
import 'package:flutter2/ui/repay.dart';
import 'package:flutter2/utils/num.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/widget/base/base_container.dart';
import 'package:flutter2/widget/base/normal_button.dart';
import 'package:flutter2/widget/base/normal_text.dart';
import 'package:flutter2/widget/info_view.dart';
import 'package:get/get.dart';

class ItemOrder extends StatelessWidget {
  const ItemOrder(this.order, {super.key});

  final OrderEntity order;

  @override
  Widget build(BuildContext context) {
    return BaseContainer(
      width: double.infinity,
      radius: 10,
      background: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
      margin: EdgeInsets.symmetric(horizontal: 16, vertical: 5),
      child: Column(
        children: [
          Row(
            children: [
              BaseContainer(
                backgroundImg: "ic_coin".toImage(),
                width: 25,
                height: 25,
              ),
              SizedBox(width: 10),
              Expanded(
                child: NormalText(
                  text: order.name,
                  fontWeight: FontWeight.bold,
                ),
              ),
              BaseContainer(
                padding: EdgeInsets.symmetric(horizontal: 5, vertical: 1),
                background: _getStatusBg(),
                width: null,
                child: NormalText(
                  fontSize: 12,
                  text: _getStatus(),
                  fontColor: _getStatusColor(),
                ),
              ),
            ],
          ),
          SizedBox(height: 5),
          InfoView(
            leftText: "Monto",
            rightText: order.loanAmount.formatAmount(),
          ),
          InfoView(
            leftText: "Plazos",
            rightText: order.period.toString(),
          ),
          InfoView(
            leftText: "Hora",
            rightText: order.loanTime,
          ),
          Offstage(
            offstage: order.overDays <= 0,
            child: InfoView(
              leftText: "Días de Vencido",
              rightText: order.overDays.toString(),
            ),
          ),
          Offstage(
            offstage: !_see(),
            child: Align(
              alignment: Alignment.topRight,
              child: NormalButton(
                click: order.status == 3 || order.status == 4 ? _repay : _change,
                radius: 5,
                margin: EdgeInsets.only(top: 8),
                height: 25,
                text: order.status == 5 ? "Por actualizar" : "Pagar",
              ),
            ),
          )
        ],
      ),
    );
  }

  _repay() {
    appssid = order.appssid;
    userId = order.userId;
    Get.to(RepayPage(order: order));
  }

  _change() {
    Get.to(AddCardPage(
      loan: false,
      orderEntity: order,
    ));
  }

  bool _see() {
    return order.status == 3 || order.status == 4 || order.status == 5;
  }

  Color? _getStatusBg() {
    Color color = tipColor;
    if (order.status == 1) {
      color = "FFF6E9".toColor();
    } else if (order.status == 2) {
      color = tipColorDark;
    } else if (order.status == 3) {
      color = "14FF5219".toColor();
    }
    return color;
  }

  Color? _getStatusColor() {
    Color color = btnColor;
    if (order.status == 1) {
      color = "FF9200".toColor();
    } else if (order.status == 2) {
      color = mainText;
    } else if (order.status == 3) {
      color = "FF5219".toColor();
    }
    return color;
  }

  String _getStatus() {
    String status = "";
    if (order.status == 0 || order.status == 6) {
      status = "Revisando";
    } else if (order.status == 1) {
      status = "Rechazado";
    } else if (order.status == 2) {
      status = "Liquidado";
    } else if (order.status == 3) {
      status = "Vencido";
    } else if (order.status == 4) {
      status = "Pendiente";
    } else if (order.status == 5) {
      status = "Por actualizar";
    }
    return status;
  }
}
